#coding:utf-8
import requests
from parsel import Selector
import js2xml
url='http://huaban.com/favorite/beauty/'
params={

'max':'',
'limit':'100',

}
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

}
headers['Accept']='application/json'
headers['X-Request']='JSON'
headers['X-Requested-With']='XMLHttpRequest'
z=requests.get(url=url,params=params,headers=headers)

def getpins():
    pins_list=[]
    while True:
        try:
            z = requests.get(url, params=params, headers=headers)
            for x in z.json()['pins']:
                # print x['pin_id']
                pins_list.append(x['pin_id'])
            last_pin_id = z.json()['pins'][-1]['pin_id']
            params['max'] = last_pin_id
            print 'last_pin_id ', last_pin_id
            if len(pins_list) == 1000:
                return pins_list

        except:
            break
    return pins_list

def getimgsrc(pin_id):
    list=[]
    url="http://huaban.com/pins/"+pin_id

    r = requests.get(url, headers=headers)
    for i in r.json()['pin']['board']['pins']:
        #print i['file']['key']
        src = "http://img.hb.aicdn.com/"
        src+=i['file']['key']
        list.append(src)
    return list

def getimgsrc1(pin_id):
    list=[]
    url="http://huaban.com/pins/"+pin_id
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

    }
    r = requests.get(url, headers=headers)

    sel=Selector(text=r.text)
    print sel
    jscode=sel.xpath("//script[contains(., 'app.page = app.page')]/text()").extract_first()
    js=js2xml.parse(jscode)

    for i in js.xpath('//property[@name="pins"]//property[@name="key"]/string/text()'):
        #print i['file']['key']
        src = "http://img.hb.aicdn.com/"
        src+=i
        list.append(src)

    return list

def downloadimg(listurl):
    for url in listurl:
        r = requests.get(url)
        imgname = url.split('/')[-1]
        with open('d:/huaban/%s.jpg' % imgname, 'wb')as img:
            img.write(r.content)



# print len(getpins())
# print downloadimg(getimgsrc1('1025893354'))
# print downloadimg(getimgsrc('1097698786'))

data=getpins()
for id in data:
    downloadimg(getimgsrc(str(id)))